package org.jeecg.wms.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.wms.entity.WmsMatheadCfg;
import org.jeecg.wms.entity.WmsPartdataCfg;
import org.jeecg.wms.service.IWmsMatheadCfgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags="来料检测配置主数据")
@RestController
@RequestMapping("/wms/wmsMatheadCfg")
@Slf4j
public class WmsMatheadCfgController extends JeecgController<WmsMatheadCfg, IWmsMatheadCfgService> {

    @Autowired
    private IWmsMatheadCfgService wmsMatheadCfgService;

    @Autowired
    private CommonAPI commonAPI;

    /**
     * 分页列表查询
     *
     * @param wmsMatheadCfg
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "原材料主数据-分页列表查询")
    @ApiOperation(value="来料检测配置-分页列表查询", notes="来料检测配置-分页列表查询")
    @GetMapping(value = "/list")
    @DS("WMS")
    public Result<IPage<WmsMatheadCfg>> queryPageList(WmsMatheadCfg wmsMatheadCfg,
                                                       @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                       @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                       HttpServletRequest req) {
        QueryWrapper<WmsMatheadCfg> queryWrapper = QueryGenerator.initQueryWrapper(wmsMatheadCfg, req.getParameterMap());
        queryWrapper.orderByDesc("CTIME");
        Page<WmsMatheadCfg> page = new Page<WmsMatheadCfg>(pageNo, pageSize);
        IPage<WmsMatheadCfg> pageList = wmsMatheadCfgService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     *   添加
     *
     * @param wmsMatheadCfg
     * @return
     */
    @AutoLog(value = "来料检测配置-添加")
    @ApiOperation(value="来料检测配置-添加", notes="来料检测配置-添加")
    //@RequiresPermissions("org.jeecg:wms_partdata_cfg:add")
    @PostMapping(value = "/add")
    @DS("WMS")
    public Result<String> add(@RequestBody WmsMatheadCfg wmsMatheadCfg) {
        Integer counterValue = commonAPI.getCounterValue("recid.wms_mathead_cfg","0");
        wmsMatheadCfg.setRecId(counterValue);
        wmsMatheadCfgService.save(wmsMatheadCfg);
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     *
     * @param wmsMatheadCfg
     * @return
     */
    @AutoLog(value = "来料检测配置-编辑")
    @ApiOperation(value="来料检测配置-编辑", notes="来料检测配置-编辑")
    //@RequiresPermissions("org.jeecg:wms_partdata_cfg:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    @DS("WMS")
    public Result<String> edit(@RequestBody WmsMatheadCfg wmsMatheadCfg) {
        wmsMatheadCfgService.updateById(wmsMatheadCfg);
        return Result.OK("编辑成功!");
    }

    /**
     *   通过选中对象删除
     *
     * @param wmsMatheadCfg
     * @return
     */
    @AutoLog(value = "来料检测配置-通过id删除")
    @ApiOperation(value="来料检测配置-通过id删除", notes="来料检测配置-通过id删除")
    //@RequiresPermissions("org.jeecg:wms_partdata_cfg:delete")
    @RequestMapping(value = "/delete")
    @DS("WMS")
    public Result<String> delete(@RequestBody WmsMatheadCfg wmsMatheadCfg) {
        wmsMatheadCfgService.removeById(wmsMatheadCfg);
        return Result.OK("删除成功!");
    }

    /**
     *  批量删除
     *
     * @param list
     * @return
     */
    @AutoLog(value = "来料检测配置-批量删除")
    @ApiOperation(value="来料检测配置-批量删除", notes="来料检测配置-批量删除")
    //@RequiresPermissions("org.jeecg:wms_partdata_cfg:deleteBatch")
    @RequestMapping(value = "/deleteBatch")
    @DS("WMS")
    public Result<String> deleteBatch(@RequestBody List<WmsMatheadCfg> list) {
        list.forEach(item->{
            wmsMatheadCfgService.removeById(item);
        });
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "原材料主数据-通过id查询")
    @ApiOperation(value="来料检测配置-通过id查询", notes="来料检测配置-通过id查询")
    @GetMapping(value = "/queryById")
    @DS("WMS")
    public Result<WmsMatheadCfg> queryById(@RequestParam(name="id",required=true) String id) {
        WmsMatheadCfg wmsMatheadCfg = wmsMatheadCfgService.getById(id);
        if(wmsMatheadCfg==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(wmsMatheadCfg);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param wmsMatheadCfg
     */
    //@RequiresPermissions("org.jeecg:wms_partdata_cfg:exportXls")
    @RequestMapping(value = "/exportXls")
    @DS("WMS")
    public ModelAndView exportXls(HttpServletRequest request, WmsMatheadCfg wmsMatheadCfg) {
        return super.exportXls(request, wmsMatheadCfg, WmsMatheadCfg.class, "来料检测配置主数据");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("wms_partdata_cfg:importExcel")
    @DS("WMS")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, WmsMatheadCfg.class);
    }

}
